(function () {
    var WaterLine = (function (_super) {
        var Pool = [];
        var RES = {
            CENTER      : "res/game/water/center_normal.png",
            EXTRA       : "res/game/water/line_extra.png",
            HEAD        : "res/game/water/line_head.png",
            CENTER_BOOM : "res/game/water/center_boom.png",
            LINE_BOOM   : "res/game/water/line_boom.png"
        };
        function WaterLine(type, direction, index, callback) {
            WaterLine.super(this);

            this._interval = 100;
            this._clipWidth = 72;
            this._clipHeight = 72;
            this._clipX = 4;
            this._clipY = 1;

            this.size(72, 72);
            this.pivot(36, 36);

            this.callback = null;
            this.isChangedSkin = false;
            this.type = null;

            this.init(type, direction, index, callback);
        }
        Laya.class(WaterLine, "", _super);
        var _proto = WaterLine.prototype;
        _proto.destroy = function () {
            _super.prototype.destroy.call(this, true);

            this._interval = null;
            this._clipWidth = null;
            this._clipHeight = null;
            this._clipX = null;
            this._clipY = null;
            this.callback = null;
            this.isChangedSkin = null;
            this.type = null;
        }

        _proto.init = function (type, direction, index, callback) {
            this.callback = callback;
            this.setAttr(direction, index);
            this.setSkin(type);
        }
        _proto.setAttr = function (direction, index) {
            switch(direction){
                case "center":
                    this.rotation = 0;
                    this.pos(0, 0);
                    break;
                case "up":
                    this.rotation = 0;
                    this.pos(0, - 72 * (index + 1));
                    break;
                case "down":
                    this.rotation = 180;
                    this.pos(0, 72 * (index + 1));
                    break;
                case "left":
                    this.rotation = -90;
                    this.pos(- 72 * (index + 1), 0);
                    break;
                case "right":
                    this.rotation = 90;
                    this.pos(72 * (index + 1), 0);
                    break;
            }
        }
        _proto.setSkin = function (type) {
            this.type = type;
            switch(type){
                case "center":
                    this.skin = RES.CENTER;
                    break;
                case "extra":
                    this.skin = RES.EXTRA;
                    break;
                case "head":
                    this.skin = RES.HEAD;
                    break;
            }
            
            console.log("WaterLine SetSkin", this.skin);
            this.callLater(this.play);
        }
        _proto.changeSkin = function () {
            switch(this.type){
                case "center":
                    this.skin = RES.CENTER_BOOM;
                    break;
                case "extra":
                case "head":
                    this._clipY = 2;
                    this.skin = RES.LINE_BOOM;
                    break;
            }
            console.log("WaterLine ChangeSkin", this.skin);
            this.callLater(this.play);
        }
        _proto.recover = function () {
            this.removeSelf();
            this.rotation = 0;
            this.pos(0, 0);
            this._clipY = 1;
            this._index = 0;
            this.skin = null;
            this.type = null;
            this.callback = null;
            this.isChangedSkin = false;

            this.callLater(function () {
                console.log("WaterLine Recovered");
                Pool.push(this);
            });
        }
        _proto.reset = function (type, direction, index, callback) {
            console.log("WaterLine Reset");
            this.init(type, direction, index, callback);
            return this;
        }

        Laya.getset(0, _proto, "index", _super.prototype._$get_index, function (value) {
            _super.prototype._$set_index.call(this, value);

            if(this._index == this._sources.length - 1){
                this.stop();
                if(this.isChangedSkin){
                    this.stop();
                    this.callback();

                    Laya.timer.once(this._interval, this, this.recover);
                    return;
                }
                this.isChangedSkin = true;
                Laya.timer.once(this._interval, this, this.changeSkin);
            }
        });

        WaterLine.create = function (type, direction, index, callback) {
            if(Pool.length !== 0){
                console.log("WaterLine Created from %cpool", "color:#f00");
                return Pool.pop().reset(type, direction, index, callback);
            }else{
                console.log("WaterLine Created from %cnew", "color:#f00");
                return new WaterLine(type, direction, index, callback);
            }
        }
        WaterLine.clear = function () {
            for(var i in Pool){
                Pool[i].destroy();
                delete Pool[i];
            }
        }

        return WaterLine;
    })(Laya.Clip);

    (function (_super) {
        var Pool = [];
        function Bubble(data) {
            Bubble.super(this);

            this._bubble = null;
            this.totalLineNum = 0;
            this.completedLineNum = 0;

            this.init();
            this.push(data);
        }
        Laya.class(Bubble, "Game.Bubble", _super);
        var _proto = Bubble.prototype;
        _proto.destroy = function () {
            _super.prototype.destroy.call(this, true);

            this._bubble = null;
            this.totalLineNum = null;
            this.completedLineNum = null;
        }

        _proto.init = function () {
            this._bubble = utils.createSkeleton("res/game/role/bubble");
            this.addChild(this._bubble);
        }
        _proto.checkFinished = function () {
            this.completedLineNum++;
            if(this.completedLineNum == this.totalLineNum){
                this.recover();
            }
        }
        _proto.push = function (data) {
            this.x = data.point.x * GAME_MOVE_RATE;
            this.y = data.point.y * GAME_MOVE_RATE;

            this._bubble.visible = true;
            this._bubble.play("wait", true);

            console.log("Bubble Pushed, Position:x" + data.point.x + ",y:" + data.point.y);
        }
        _proto.createLine = function (direction, range) {
            var angle = null;
            var pos = null;
            var maxSize = range.size;
            var isBuilding = range.building;

            for(var i = 0; i < maxSize; i ++){
                var type = "extra";
                if(i == maxSize - 1){
                    if(!isBuilding){
                        type = "head";
                    }else{
                        continue;
                    }
                }
                this.totalLineNum++;

                this.addChild(WaterLine.create(type, direction, i, this.checkFinished.bind(this)));
            }
            console.log("Bubble Create Lines, Total Num:" + this.totalLineNum);
        }
        _proto.boom = function (range) {
            this.zOrder = 1;
            this._bubble.stop();
            this._bubble.visible = false;
            var x = this.x;
            var y = this.y;

            this.totalLineNum++;
            this.addChild(WaterLine.create("center", "center", 0, this.checkFinished.bind(this)));

            for(var item in range){
                this.createLine(item, range[item]);
            }
        }
        _proto.recover = function () {
            this.totalLineNum = 0;
            this.completedLineNum = 0;
            this.removeSelf();

            this.callLater(function () {
                console.log("Bubble Recovered");
                Pool.push(this);
            });
        }
        _proto.reset = function (data) {
            this.push(data);
            return this;
        }

        Bubble.create = function (data) {
            if(Pool.length !== 0){
                console.log("Bubble Created from %cpool", "color:#00f");
                return Pool.pop().reset(data);
            }else{
                console.log("Bubble Created from %cnew", "color:#00f");
                return new Bubble(data);
            }
        }
        Bubble.clear = function () {
            for(var i in Pool){
                Pool[i].destroy();
                delete Pool[i];
            }
        }
    })(Laya.Box);
})();